/* ----------------------------------------------------------------------------------------------------------------
 * AbstractSection
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.renderer.sections 
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import pure.engine.renderer.RenderUtil;
	
	import pure.debug.Logger;
	import pure.engine.system.ns_despair;
	import pure.utils.DesN;
	
	use namespace ns_despair;
	
	/**
	 * @internal
	 * 
	 * 抽象片断
	 * 
	 * @author ＃Pure
	 * 
	 */
public class Section
{
	public function Section(name:String, source:*, x:Number, y:Number, realRect:Rectangle)
	{
		_parseAssets(source);
		
		if (name == null || name == '')
		{
			Logger.reportError(this,'Constructor','片段名称不可为空');
		}
		
		m_name              =  name;
		m_x                 =  x;
		m_y                 =  y;
		m_realRect          =  realRect;
		m_realRect.x       +=  m_x;
		m_realRect.y       +=  m_y;
		
		m_range = RenderUtil.getRange(x, y, m_width, m_height);
	}
	
	

	ns_despair var m_bitmaps:Vector.<BitmapData>;    // 位图列表，第一个为空
	
	ns_despair var m_actionMap:Dictionary;
	
	ns_despair var m_realRect:Rectangle;

	ns_despair var m_maxLength:int;   // 最大位图数（swf总帧数 * 2 + 初始空白帧 * 1）

	ns_despair var m_x:Number, m_y:Number, m_width:Number, m_height:Number, m_range:Number;

	ns_despair var m_name:String;

	
	
	
	ns_despair function getBitmapByPointer(pointer:int):BitmapData 
	{
		var tmpPointer:int;
		
		tmpPointer = (pointer < 0) ? this.m_maxLength + pointer : pointer;
		if (!m_bitmaps[tmpPointer]) 
		{
			_preDraw(pointer);
		}
		return this.m_bitmaps[tmpPointer];
	}
	
	
	ns_despair function getActionByName(actionName:String):Action
	{
		return m_actionMap[actionName];
	}

	
	ns_despair function addAction(actionName:String, data:String, reversed:Boolean, frameRate:int ):void 
	{	
		if (!m_actionMap) 
		{
			m_actionMap = new Dictionary();
		}
		
		var parsedData:Array;
		var action:Action;
		var len:int, i:int;
		var group:Vector.<int>;

		parsedData  =  _parseActionData(data);
		len         =  parsedData.length;
		
		if (reversed)
		{
			for (i = 0; i < len; i++)
			{
				parsedData[i] = ~parsedData[i] + 1;
			}
		}

		group = new Vector.<int>(len, true);
		while (--len > -1)
		{
			group[len] = parsedData[len];
		}
		
		action                  =  new Action(actionName, group, frameRate);
		m_actionMap[actionName]  =  action;
	}
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	

	
	/**
	 * 解析XML数据
	 * 
	 * @param data
	 */
	protected function _parseActionData(data:String):Array 
	{
		var group:Array, tmpArr:Array, list:Array;
		var len:int, i:int, tmpPointer:int;
		
		group = [];
		
		if (data.indexOf(",") != -1) 
		{
			list  =  data.split(",");
			len   =  list.length;
			
			for (i = 0; i < len; i++)
			{
				data = list[i];
				group.push.apply(group, _parseActionData(data));
			}
		}
		
		else 
		{
			if (data.indexOf('-') != -1)
			{
				tmpArr     =  data.split("-");
				tmpArr[0]  =  int(tmpArr[0]);
				tmpArr[1]  =  int(tmpArr[1]);
				// 正向
				if (tmpArr[0] < tmpArr[1])
				{
					while (tmpArr[0] <= tmpArr[1]) 
					{
						group.push(tmpArr[0]);
						tmpArr[0]++;
					}
				}
				// 反向
				else
				{
					while (tmpArr[0] >= tmpArr[1]) 
					{
						group.push(tmpArr[0]);
						tmpArr[0]--;
					}
				}
			}
			
			else if (data.indexOf('*') != -1)
			{
				tmpArr      =  data.split("*");
				tmpPointer  =  tmpArr[0];
				
				for (i = 0; i < int(tmpArr[1]); i++)
				{
					group.push(tmpPointer);
				}
			}
			
			else
			{
				group.push(int(data));
			}
		}
		
		return group;
	}
    

	
	/**
	 * @inheritDoc
	 * 
	 * 解析资源
	 * 
	 * @param assets
	 */
	protected function _parseAssets(assets:*):void 
	{
		// Override !!
	}
	
	
	/**
	 * @inheritDoc
	 * 
	 * 预绘制
	 * 
	 * @param pointer
	 */
	protected function _preDraw(pointer:int):void 
	{	
		// Override !!
	}

}

}